package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Notice;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.NoticeMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UserMapper userMapper;

    public void save(Long noticeId, Long noticedId) {
        noticeMapper.save(noticeId, noticedId);
        User noticeUser = userMapper.selectByPrimaryKey(noticeId);
        User noticedUser = userMapper.selectByPrimaryKey(noticedId);
        noticeUser.setCares(noticeUser.getCares()+1);
        userMapper.updateCaresAndFans(noticeUser);
        noticedUser.setFans(noticedUser.getFans()+1);
        userMapper.updateCaresAndFans(noticedUser);

    }

    public Notice selectOne(Long noticeId, Long noticedId) {

        Notice notice = noticeMapper.selectOne(noticeId, noticedId);
        if (notice == null) {
            throw new RuntimeException();
        } else {
            return notice;
        }
    }

    public void delete(Long noticeId, Long noticedId) {
        noticeMapper.delete(noticeId, noticedId);
        User noticeUser = userMapper.selectByPrimaryKey(noticeId);
        User noticedUser = userMapper.selectByPrimaryKey(noticedId);
        noticeUser.setCares(noticeUser.getCares()-1);
        userMapper.updateCaresAndFans(noticeUser);
        noticedUser.setFans(noticedUser.getFans()-1);
        userMapper.updateCaresAndFans(noticedUser);
    }

    public Integer getNoticeNum(Long id) {
        return noticeMapper.getNoticeNum(id);
    }

    public Integer getNoticedNum(Long id) {
        return noticeMapper.getNoticedNum(id);
    }
}
